var request = null;

function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
	if (!request) {
		alert("Error initializing XMLHttpRequest!");
	}
}

function sendRequest(method, url, headerName, headerValue, parameter, func) {
	request.open(method, url, true);
	request.onreadystatechange = func;
	request.setRequestHeader(headerName, headerValue);
	request.send(parameter);
}

function getResponse() {
	var response = null;
	if (request.readyState == 4) {
		if (request.status == 200) {
			response = request.responseText;
		} else
			alert("status is " + request.status);
	}
	return response;
}

function loadComplaintTypeList() {
	var method = "POST";
	var url = "GetComplaintTypeListBySyndromeId";
	var headerName = "Content-Type";
	var headerValue = "application/x-www-form-urlencoded";
	var syndrome = document.getElementById("syndrome");
	var syndromeId = syndrome.value;
	var parameter = "syndromeId=" + encodeURIComponent(syndromeId);
	createRequest();
	sendRequest(method, url, headerName, headerValue, parameter, fillSelect);
}

function fillSelect() {
	var jsonObject = getResponse();
	var select = document.getElementById("complaintType");
	for ( var i = select.options.length - 1; i >= 0; i--) {
		select.remove(i);
	}
	var complaintTypeList = JSON.parse(jsonObject);
	for ( var i = 0; i < complaintTypeList.length; i++) {
		select.options[select.options.length] = new Option(
				complaintTypeList[i].name, complaintTypeList[i].id);
	}
}

function loadComplaintList() {
	var method = "POST";
	var url = "GetComplaintListBySyndromeId";
	var headerName = "Content-Type";
	var headerValue = "application/x-www-form-urlencoded";
	var syndrome = document.getElementById("syndrome");
	var syndromeId = syndrome.value;
	var parameter = "syndromeId=" + encodeURIComponent(syndromeId);
	createRequest();
	sendRequest(method, url, headerName, headerValue, parameter,
			fillComplaintForm);
}

function fillComplaintForm() {

	var jsonObject = getResponse();

	if (jsonObject != null) {
		var form = document.getElementById("mainForm");
		var childNodes = form.childNodes;
		var childNodesLength = childNodes.length;
		for ( var i = childNodesLength - 1; i >= 0; i--) {
			form.removeChild(childNodes[i]);
		}

		var parsedJsonObject = JSON.parse(jsonObject);

		var complaintTypeListObject = parsedJsonObject.complaintTypes;
		var complaintTypeListLength = complaintTypeListObject.length;

		var br = null;
		var label = null;
		var radio = null;

		if (complaintTypeListLength > 0) {
			var div = document.getElementById("surveyMenu");
			div.style.display = "block";
		} else {
			var div = document.getElementById("surveyMenu");
			div.style.display = "none";
		}

		for ( var i = 0; i < complaintTypeListLength; i++) {
			var complaintTypeName = complaintTypeListObject[i].name;
			var complaintListObject = complaintTypeListObject[i].complaints;
			var complaintListLength = complaintListObject.length;

			var ul = document.createElement("ul");
			ul.setAttribute("class", "ul");
			var li = document.createElement("li");
			li.setAttribute("class", "li_title");
			li.innerHTML = complaintTypeName;
			ul.appendChild(li);
			form.appendChild(ul);
			var table = document.createElement("table");
			table.setAttribute("class", "table");
			for ( var j = 0; j < complaintListLength; j++) {
				var complaintId = complaintListObject[j].id;
				var complaintName = complaintListObject[j].name;
				var row = table.insertRow(-1);
				row.setAttribute("class", "table");
				cell0 = row.insertCell(-1);
				cell1 = row.insertCell(-1);
				var ul = document.createElement("ul");
				ul.setAttribute("class", "ul");
				var li = document.createElement("li");
				li.setAttribute("class", "li_line");
				li.innerHTML = complaintName;
				ul.appendChild(li);
				if (complaintName == "Інше") {
					var div = document.createElement("div");
					div.style.display = "inline-block";
					var text = document.createElement("input");
					text.setAttribute("class", "text");
					text.setAttribute("maxlength", "45");
					text.type = "text";
					text.name = "description" + complaintId;
					div.appendChild(ul);
					div.appendChild(text);
					cell0.appendChild(div);
				} else {
					cell0.appendChild(ul);
				}
				cell1.setAttribute("class", "radioBlock");
				for ( var k = 0; k < 4; k++) {
					radio = document.createElement("input");
					radio.type = "radio";
					radio.name = complaintId;
					radio.value = k;
					if (k == 0) {
						radio.checked = true;
					}
					cell1.appendChild(radio);
					label = document.createElement("label");
					label.innerHTML = k;
					cell1.appendChild(label);
				}
			}
			form.appendChild(table);
			br = document.createElement("br");
			form.appendChild(br);
		}

		if (complaintTypeListLength > 0) {
			var submit = document.createElement("input");
			submit.type = "submit";
			submit.name = "save";
			submit.value = "Зберегти";
			form.appendChild(submit);
		}
		
	}

}